package org.UDPchatRoomDemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class receiveMessageDemo {
    public static void main(String[] args) throws IOException {
        int people=0;

        DatagramSocket datagramSocket=new DatagramSocket(10086);

        DatagramPacket datagramPacket=new DatagramPacket(new byte[1024],1024);
        while (true) {
            datagramSocket.receive(datagramPacket);
            people++;
            byte[] data = datagramPacket.getData();
            int length = datagramPacket.getLength();
            String dataStr=new String(data,0,length);
            if (people%2==0){
                System.out.println("A说:"+dataStr);
            }else {
                System.out.println("B说:"+dataStr);
            }
            if (dataStr.equals("bye")){
                break;
            }
        }
        datagramSocket.close();
    }
}
